// store/modules/dictionary.js
import { getDicts } from "@/api/index";
const state = {
  dicts: [],
};

const getters = {
  allDictionaries(state) {
    return state.dicts; // 返回所有字典数据
  },
  getDictByType: (state) => (type) => {
    const dict = state.dicts.find((dict) => dict.dictType === type);
    return dict ? dict.dataList : [];
  },
};

const mutations = {
  setDicts(state, dicts) {
    state.dicts = dicts;
  },
};

const actions = {
  async fetchDicts({ commit }) {
    try {
      if (localStorage.getItem("token")) {
        const { data } = await getDicts(); // 请求字典数据
        if (data.code === 0) {
          commit("setDicts", data.data); // 存储字典数据到 state
        }
      }
    } catch (error) {
      console.error("获取字典数据失败", error);
    }
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
